package com.ocr.utils;

import com.googlecode.tesseract.android.TessBaseAPI;
import com.ocr.MainActivity;
import com.ocr.R;
import com.ocr.entities.Language;
import com.ocr.entities.LanguageContext;
import com.ocr.fragment.FragmentScan;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class OCRAsyntask extends AsyncTask<Bitmap, Void, String> {
	public static Bitmap bitmapTest = null;
	private Handler handler;
	private Handler handlerWhileProcess;
	private TessBaseAPI tessBaseAPI;
	private String dataPath;
	public static Language SELECTED_LANG;
	private Activity activity;

	public OCRAsyntask(Activity activity, Handler handler,
			TessBaseAPI tessBaseAPI, String dataPath,
			Handler handlerWhileProcess) {
		super();
		this.handler = handler;
		this.handlerWhileProcess = handlerWhileProcess;
		this.tessBaseAPI = tessBaseAPI;
		this.dataPath = dataPath;
		this.activity = activity;
		SELECTED_LANG = LanguageContext.getListLanguages().get(
				MainActivity.SETTINGITEM.getSelectedLanguagePos());// get
																	// selected
		// language
	}

	@Override
	protected String doInBackground(Bitmap... params) {
		// TODO Auto-generated method stub
		try {
			Bitmap bitmap = null;
			if (MainActivity.SETTINGITEM.isAutoProcessImg() == true) {
				// set dialog watting text
				Message msg = Message.obtain();
				msg.obj = activity.getResources().getString(
						R.string.ocr_status_processing_img);
				handlerWhileProcess.sendMessage(msg);
				// process image
				ProcessImgUtil imgUtil = new ProcessImgUtil();
				bitmap = imgUtil.processImg(params[0]);
			} else
				bitmap = params[0];
			bitmapTest = bitmap;
			// set dialog watting text
			Message msg = Message.obtain();
			msg.obj = activity.getResources().getString(
					R.string.ocr_status_processing_text);
			handlerWhileProcess.sendMessage(msg);
			// ocr
			tessBaseAPI.init(SELECTED_LANG.getPath(),
					SELECTED_LANG.getLanguageCode(), TessBaseAPI.OEM_DEFAULT);
			tessBaseAPI.setImage(bitmap);
			String result = tessBaseAPI.getUTF8Text();
			Log.d(ApplicationConstant.LOGT_TAG, "--OCR result:" + result);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		FragmentScan.layoutStatus.setVisibility(View.GONE);
		Message msg = Message.obtain();
		msg.obj = result;
		handler.sendMessage(msg);
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		FragmentScan.disableAllView();
		FragmentScan.layoutStatus.setVisibility(View.VISIBLE);
	}
}
